package pl.edu.pw.perg.compinfodb;

import org.acegisecurity.GrantedAuthorityImpl;
import org.acegisecurity.userdetails.User;
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.springframework.dao.DataAccessException;

import pl.edu.pw.perg.compinfodb.manager.IUserManager;

public class EqdbUserDetailsService implements UserDetailsService {

	private IUserManager userManager;
	
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		pl.edu.pw.perg.compinfodb.entities.User user = userManager.findByLogin(username);
		if(user == null)
			throw new UsernameNotFoundException("User: " + username + " not found.");
		UserDetails userDetails = new User(user.getLogin(), user.getPassword(), true, true, true, true,
				new GrantedAuthorityImpl[] { new GrantedAuthorityImpl(user.getRole()) });
		return userDetails;
	}

	public void setUserManager(IUserManager userManager) {
		this.userManager = userManager;
	}
}
